//
// Created by zhuanding on 2023/5/25.
//

#include "../01-preface/Status.h"

#define MaxSize 50
typedef struct {
    ElemType data[MaxSize];   //存放队列元素
    int front, rear;     //队头指针和队尾指针
} SqQueue;


void InitQueue_Sq(SqQueue &Q) {
    Q.front = Q.rear = 0;
}

int QueueLength_Sq(SqQueue Q) {
    return (Q.rear - Q.front + MaxSize) % MaxSize;
}

bool QueueEmpty_Sq(SqQueue Q) {
    return Q.front == Q.rear;
}

bool EnQueue_Sq(SqQueue &Q, ElemType e) {
    if ((Q.rear + 1) % MaxSize == Q.front)  // 队满，报错
        return false;
    Q.data[Q.rear] = e;
    Q.rear = (Q.rear + 1) % MaxSize;  // 队尾指针加1取模
    return true;
}

bool DeQueue_Sq(SqQueue &Q, ElemType &e) {
    if (Q.front == Q.rear) return false;
    e = Q.data[Q.front];
    Q.front = (Q.front + 1) % MaxSize;   // 队头指针加1取模
    return true;
}

bool GetHead(SqQueue Q, ElemType &e) {
    if (Q.front == Q.rear) return false;
    e = Q.data[Q.front];
    return true;
}


int main() {
    SqQueue Q;

    ElemType e;
    int a[10] = {0};

    printf("***********初始化顺序队列***********\n");
    InitQueue_Sq(Q);
    printf(">>>>>初始化成功！表长为%d\n", QueueLength_Sq(Q));
    printf("顺序表L的地址是%p\n", &Q);
    printf("\n");

    printf("***********顺序队列插入元素***********\n");
    for (int i = 1; i <= 10; i++) {
        a[i] = i * i;
        if (EnQueue_Sq(Q, a[i])) {
            printf("元素%d入队成功！\n", a[i]);
            GetHead(Q, e);
            if (e)
                printf("队首元素为:%d\n", e);
            else
                printf("取队首元素失败！\n");
            printf("当前顺序队列的长度为：%d\n", QueueLength_Sq(Q));
            printf("--------\n");
        } else {
            printf("入队失败!\n");
        }
    }
    printf("\n");

    printf("***********顺序队列元素出队***********\n");
    while (!QueueEmpty_Sq(Q)) {
        DeQueue_Sq(Q, e);
        if (e)
            printf("%d元素出队\n", e);
        else
            printf("出队失败！\n");
    }
    printf("\n");

    return 0;
}
